package com.quyeying.service.impl;

import com.quyeying.entity.TblPopularizeVideoEntity;
import com.quyeying.entity.TblPraiseEntity;
import com.quyeying.entity.TblProductEntity;
import com.quyeying.mapper.TblPraiseDao;
import com.quyeying.service.TblPopularizeVideoService;
import com.quyeying.service.TblPraiseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quyeying.utils.TokenUtil;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
* <p>
 * 点赞表 服务实现类
 * </p>
*
* @author jll
* @since 2022-10-25
*/
@Service
public class TblPraiseServiceImpl extends ServiceImpl<TblPraiseDao, TblPraiseEntity> implements TblPraiseService {


    @Resource
    private TblPopularizeVideoService tblPopularizeVideoService;

    @Override
    public R findListByPage(Integer page, Integer pageCount){
        IPage<TblPraiseEntity> wherePage = new Page<>(page, pageCount);
        TblPraiseEntity where = new TblPraiseEntity();
        IPage<TblPraiseEntity> iPage = baseMapper.selectPage(wherePage, Wrappers.query(where));
        return R.success(ReturnPageData.fillingData(iPage));
    }

    @Override
    public R add(Map<String, Long> map, HttpServletRequest request){
        long userId = TokenUtil.getUserId(request);
        TblPraiseEntity tblPraiseEntity = new TblPraiseEntity();
        tblPraiseEntity.setUserId(userId);
        tblPraiseEntity.setVideoId(map.get("id"));
        TblPopularizeVideoEntity videoServiceById = tblPopularizeVideoService.getById(tblPraiseEntity.getVideoId());
        TblPopularizeVideoEntity newTblPopularizeVideoEntity = new TblPopularizeVideoEntity();
        newTblPopularizeVideoEntity.setId(map.get("id"));
        try {
            baseMapper.insert(tblPraiseEntity);
            //增加点赞数
            newTblPopularizeVideoEntity.setAgreeNum(videoServiceById.getAgreeNum()+1);
            tblPopularizeVideoService.updateData(newTblPopularizeVideoEntity);
        }catch (DuplicateKeyException duplicateKeyException){
            baseMapper.delete(Wrappers.query(tblPraiseEntity));
            //点赞数-1
            newTblPopularizeVideoEntity.setAgreeNum(videoServiceById.getAgreeNum()-1);
            tblPopularizeVideoService.updateData(newTblPopularizeVideoEntity);
        }
        return R.success();
    }

    @Override
    public R delete(Long id){
        baseMapper.deleteById(id);
        return R.success();
    }

    @Override
    public R updateData(TblPraiseEntity tblPraiseEntity){
        baseMapper.updateById(tblPraiseEntity);
        return R.success();
    }

    @Override
    public R findById(Long id){
        TblPraiseEntity tblPraiseEntity = baseMapper.selectById(id);
        return R.success(tblPraiseEntity);
    }
}
